/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div class="item-title">
      <div class="subject">
        <h3>新人优惠券</h3>
        <h5>商城新人优惠券设定与管理</h5>
      </div>
    </div>
    <div
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>注册7天以内未下过单，并且没有领取过优惠券的用户视为新人，一个新人只允许领取一次新人优惠券.</li>
          <li>新人优惠券使用起始日期为领取时的日期，发放数量无限制，新用户仅限领取一次.</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>

    <div class="mDiv">
      <div class="ftitle">
        <h3>新人优惠券列表</h3>
        <h5>(共 {{totalCount}} 条记录)</h5>
      </div>
      <div class="fbutton">
        <div class="add" @click="toAdd()">
          <span>
            <i class="el-icon-circle-plus" /> 新增优惠券
          </span>
        </div>
      </div>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
      <div class="searchbox">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="small"
        ></el-date-picker>
        <el-input
          class="searchinput"
          v-model="name"
          width="10"
          size="small"
          placeholder="优惠券名称"
          clearable
        />
        <el-button
          class="searchbtn"
          size="small"
          icon="el-icon-search"
          @click="currentPage = 1;getList()"
        >搜索</el-button>
      </div>
    </div>

    <div class="tablebox">
      <el-table :data="tableData" style="width: 100%;">
        <el-table-column label="优惠券名称" prop="name" width="220"></el-table-column>
        <el-table-column label="优惠券类型" prop="type_detail" width="120"></el-table-column>
        <el-table-column label="面额" prop="money" width="100"></el-table-column>
        <el-table-column label="使用需满金额" prop="condition" width="160"></el-table-column>
        <el-table-column label="总发行量" prop="createnum" width="120"></el-table-column>
        <el-table-column label="已发放" prop="send_num" width="100"></el-table-column>
        <el-table-column label="状态" prop="status_detail" width="100"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
              <el-button size="small" @click="couponDetails(scope)" icon="el-icon-zoom-in">查看</el-button>
              <el-button size="small" @click="couponEdit(scope)" icon="el-icon-edit-outline">编辑</el-button>
              <el-button size="small" @click="couponList(scope)" icon="el-icon-tickets">详细</el-button>
              <el-button size="small" @click="remove(scope.row.id)" icon="el-icon-delete">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        class="paginationbox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { getStoreCouponPage, deleteCoupon } from "@/utils/api";
export default {
  data() {
    return {
      tableData: [],
      currentPage: this.$route.params.currentPage || 1,
      totalCount: 0,
      pageSize: 10,
      name: "",
      dateRange: "",
      query: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getList();
    },
    handleCurrentChange(e) {
      this.currentPage = e;
      this.getList();
    },
    getList() {
      var that = this;
      this.query.p = this.currentPage;
      this.query.size = this.pageSize;
      this.query.name = this.name;
      this.query.type = 4;
      if (this.dateRange != null && this.dateRange.length > 1) {
        this.query.start_time = parseInt(this.dateRange[0].valueOf() / 1000);
        this.query.end_time = parseInt(this.dateRange[1].valueOf() / 1000);
      } else {
        this.$delete(this.query, "start_time");
        this.$delete(this.query, "end_time");
      }
      getStoreCouponPage(that.query).then(function(res) {
        that.tableData = res.records;
        that.totalCount = res.total;
      });
    },
    refresh() {
      this.getList();
      this.$message.success("刷新成功！");
    },
    couponList(scope) {
      this.$router.push({
        name: "coupon_list",
        params: {
          id: scope.row.id
        }
      });
    },
    couponDetails(scope) {
      this.$router.push({
        name: "newbie_coupons_details",
        params: {
          id: scope.row.id
        }
      });
    },
    couponEdit(scope) {
      this.$router.push({
        name: "newbie_coupons_edit",
        params: {
          id: scope.row.id
        }
      });
    },
    toAdd() {
      this.$router.push({
        name: "newbie_coupons_edit"
      });
    },
    remove(id) {
      var that = this;
      this.$confirm("是否删除该优惠卷?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteCoupon(id).then(function(res) {
            that.$message.success("删除成功!");
            that.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped>
.searchinput {
  width: auto;
}
</style>
